﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace Tools.UserControls
{
    public partial class UserTask : UserControl
    {
        public string ControlName { get; set; }
        public string Title { get; set; }
        public string FolderID { get; set; }

        public List<TaskModel> TaskModels { get; set; }

        /// <summary>
        /// 委托
        /// </summary>
        /// <param name="name"></param>
        public delegate void AddDelegate(string ControlName, string title, string folderID);

        /// <summary>
        /// 添加事件
        /// </summary>
        public event AddDelegate AddEvent;

        public UserTask()
        {
            InitializeComponent();
        }

        public void InitBind()
        {
            listView1.Items.Clear();
            this.label1.Text = this.Title;

            var i = 1;
            foreach (var item in this.TaskModels)
            { 
                listView1.Items.Add($"{i}.{item.subject.ToString()}");
                i++;
            }


            //var filter = $"status ne 'completed'";
            //var orderby = $"createdDateTime desc";
            //var url = $"https://graph.microsoft.com/beta/me/outlook/taskFolders/{this.FolderID}/tasks?$filter={filter}&$orderby={orderby}&$top=999";
            //var res = Helper.MicrosoftToken.HttpRequest(url, "", "get");
            //var list = JsonConvert.DeserializeObject<dynamic>(res).value;

            //var i = 1;
            //var tasks = new List<string>();
            //foreach (var item in list)
            //{
            //    tasks.Add($"{i}.{item["subject"].ToString()}");
            //    i++;
            //}
            //foreach (var item in tasks)
            //{
            //    listView1.Items.Add(item);
            //}
        }

        private void link_refresh_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)//刷新
        {
            InitBind();
        }

        private void link_add_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)//添加
        {
            this.AddEvent(this.ControlName, this.Title, this.FolderID);
        }
    }
}
